package com.example.zy5_project_jetpack.mvp.view.SofaFragment.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Bean.ImageBean;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.Bean.VideoBean;

import java.util.ArrayList;
import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<VideoBean.DataBeanX.DataBean> dataBeans = new ArrayList<>();

    public VideoAdapter(Context context) {
        this.context = context;
    }

    public void addList(List<VideoBean.DataBeanX.DataBean> dataBeans) {
        this.dataBeans.addAll(dataBeans);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.video_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder= (ViewHolder) holder;
        VideoBean.DataBeanX.DataBean dataBean = dataBeans.get(position);
        RequestOptions options=new RequestOptions();
        options.transform(new CircleCrop());
        Glide.with(context).load(dataBean.getAuthor().getAvatar()).apply(options).into(viewHolder.iv_image_video);
        viewHolder.tv_text_video.setText(dataBean.getAuthor().getName());
        viewHolder.tv_title_video.setText(dataBean.getFeeds_text());
        viewHolder.view_video_video.setVideoPath(dataBean.getUrl());
        viewHolder.view_video_video.start();
        viewHolder.view_video_video.stopPlayback();//停止播放视频，并且释放
        viewHolder.view_video_video.suspend();//在任何状态下释放媒体资源

    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }


    public static
    class ViewHolder extends RecyclerView.ViewHolder{
        public View rootView;
        public ImageView iv_image_video;
        public TextView tv_text_video;
        public TextView tv_title_video;
        public VideoView view_video_video;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.iv_image_video = (ImageView) rootView.findViewById(R.id.iv_image_video);
            this.tv_text_video = (TextView) rootView.findViewById(R.id.tv_text_video);
            this.tv_title_video = (TextView) rootView.findViewById(R.id.tv_title_video);
            this.view_video_video = (VideoView) rootView.findViewById(R.id.view_video_video);

        }

    }
}
